home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / ProcDoggie 1.0a6⁄THINK P / UGlobals.p < prev    next >
Encoding:
Text File  |  1991-02-21  |  18.6 KB  |  552 lines  |  [TEXT/PJMM]

  1. unit UGlobals;
  2.  
  3.  
  4. {-------------------------------------------------------------------------------}
  5. {#}
  6. {#    Apple Macintosh Developer Technical Support}
  7. {#}
  8. {#    Interfaces for the application utilities}
  9. {#}
  10. {#    Program:    ProcDoggie}
  11. {#    File:        UGlobals.p - Pascal Implementation}
  12. {#}
  13. {#    by:        Forrest Tanaka}
  14. {#}
  15. {#    Copyright © 1988-1991 Apple Computer, Inc.}
  16. {#    All rights reserved.}
  17. {#}
  18. {--------------------------------------------------------------------------------}
  19. {#}
  20. {# This unit contains declarations and routines that didn’t seem to fit into any}
  21. {# other unit in this application.}
  22. {#}
  23. {-------------------------------------------------------------------------------}
  24.  
  25. {-------------------------------------------------------------------------------}
  26. {#}
  27. {#     2/21/91 pvh - THINK Pascal conversion.}
  28. {#    Notes:}
  29. {#}
  30. {#}
  31. {-------------------------------------------------------------------------------}
  32.  
  33. interface
  34.  
  35.  
  36. (*******************************************************************************}
  37. {* Used Units}
  38. {*******************************************************************************)
  39.  
  40.     uses
  41.         (* Group 1 *)
  42.         Types, QuickDraw, 
  43.  
  44.         (* Group 2 *)
  45.         Controls, Errors, Events, OSUtils, Files, GestaltEqu, Memory, Resources, TextEdit, ToolUtils, 
  46.  
  47.         (* Group 3 *)
  48.         Notification, OSEvents, Processes, Windows, 
  49.  
  50.         (* Group 4 *)
  51.         Dialogs, 
  52.  
  53.         (* Application *)
  54.         UEmergMem;
  55.  
  56.  
  57. (*******************************************************************************}
  58. {* Constants}
  59. {*******************************************************************************)
  60.  
  61.     const
  62.         rMemErrMessages = 1000; {Resource ID of memory error message STR#}
  63.         kMemErrAppOpenMsg = 1;    {Not enough memory to open application}
  64.         kMemErrProcListOpenMsg = 2;    {Not enough mem to open process list wind}
  65.         kMemErrLowMemWarnMsg = 3;    {Free memory is low; proceed with caution}
  66.         kMemErrProcInfoOpenMsg = 4;    {Not enough mem to open process info wind}
  67.  
  68.         rResErrMessages = 1001; {Resource ID of resource error message STR#}
  69.         kResErrAppDamageMsg = 1;    {Application is damaged}
  70.  
  71.         rMiscErrMessages = 1002; {Resource ID of misc. error message STR#}
  72.         kMiscErrUnknownMsg = 1;    {Unknown error}
  73.  
  74.         rMiscWrnMessages = 2000; {Resource ID of misc. warning message STR#}
  75.         kMiscWrnUncleanMsg = 1;    {Application not 32-bit clean warning}
  76.         kMiscWrnLaunchMemMsg = 2;    {Not enough memory to launch}
  77.  
  78.         kMaxSleepTime = 60; {Max WNE sleep time in ticks, 1 second in this case}
  79.  
  80.  
  81. (*******************************************************************************}
  82. {* Global Variables}
  83. {*******************************************************************************)
  84.  
  85.     var
  86.         gError: Integer; {Generic application error code}
  87.         gWereInFront: Boolean; {TRUE if this appliation is front most}
  88.         gQuitting: Boolean; {TRUE if user chose Quit command}
  89.  
  90.  
  91. (*******************************************************************************}
  92. {* DoQuit - Handle Quit command}
  93. {*}
  94. {* This routine is called when this application should quit.  It’s called}
  95. {* when the user chooses the Quit command from the file menu or when a 'quit'}
  96. {* AppleEvent is received.}
  97. {*******************************************************************************)
  98.  
  99.     procedure DoQuit;
  100.  
  101.  
  102. (*******************************************************************************}
  103. {* ShowStopAlert - Show a stop alert}
  104. {*}
  105. {* This routine puts up a standard stop alert with just an OK button and a}
  106. {* specified message.  messageClass specifies the STR# resource ID which contains}
  107. {* the message to display and messageIndex specifies the index (the first message}
  108. {* is index 1) into that STR# of the message to display.}
  109. {*******************************************************************************)
  110.  
  111.     function ShowStopAlert (messageClass: Integer; messageIndex: Integer): Integer;
  112.  
  113.  
  114. (*******************************************************************************}
  115. {* ShowCautionOKCancelAlert - Show a caution alert with an OK and Cancel button}
  116. {*}
  117. {* This routine puts up a standard caution alert with just an OK button, a Cancel}
  118. {* button, and a specified message.  messageClass specifies the STR# resource ID}
  119. {* which contains the message to display and messageIndex specifies the index}
  120. {* (the first message is index 1) into that STR# of the message to display.}
  121. {*******************************************************************************)
  122.  
  123.     function ShowCautionOKCancelAlert (messageClass: Integer; messageIndex: Integer): Integer;
  124.  
  125.  
  126. (*******************************************************************************}
  127. {* ShowCautionOKAlert - Show a caution alert with just an OK button}
  128. {*}
  129. {* This routine puts up a standard caution alert with just an OK button and a}
  130. {* specified message.  messageClass specifies the STR# resource ID which contains}
  131. {* the message to display and messageIndex specifies the index (the first message}
  132. {* is index 1) into that STR# of the message to display.}
  133. {*******************************************************************************)
  134.  
  135.     function ShowCautionOKAlert (messageClass: Integer; messageIndex: Integer): Integer;
  136.  
  137.  
  138. (*******************************************************************************}
  139. {* ShowAboutBox - Show the About box}
  140. {*}
  141. {* The About box for this application is displayed.  This is just a simple alert}
  142. {* box with an OK button.  The name of this application is displayed as is its}
  143. {* version number.}
  144. {*******************************************************************************)
  145.  
  146.     procedure ShowAboutBox;
  147.  
  148.  
  149. (*******************************************************************************}
  150. {* CreateWindow - Create a window}
  151. {*}
  152. {* This routine creates a window using the WIND resource template with a resource}
  153. {* ID of windowTmplID in front of all existing windows.  A pointer to this window}
  154. {* is returned.  If we’re running on a Color QuickDraw machine, the window is}
  155. {* created as a color window.  This window must be disposed of by calling}
  156. {* CloseWindow followed by DisposPtr on the window’s WindowRecord.  The window}
  157. {* shouldn’t be closed using DisposeWindow.  When this routine returns, the new}
  158. {* window is the current GrafPort.}
  159. {*}
  160. {* The window isn’t explicitly repositioned from its position as defined by the}
  161. {* resource definition.  Instead, I use the window-positioning utilities.  This}
  162. {* isn’t documented at this time and it isn’t even certain the these utilities}
  163. {* will be implemented in the final release of system software 7.0.  Use ResEdit}
  164. {* 2.1’s WIND editor to control the window-positioning utilities.}
  165. {*}
  166. {* If there isn’t enough memory for the window, then gError global is set to}
  167. {* memFullErr.  If the WIND resource couldn’t be loaded, gError is set to}
  168. {* resNotFound.  If anything else went wrong while creating the window, then}
  169. {* gError is set to dsSysErr.}
  170. {*******************************************************************************)
  171.  
  172.     function CreateWindow (windowTmplID: Integer): WindowPtr;
  173.  
  174.  
  175. (*******************************************************************************}
  176. {* CreateDialog - Create a dialog window}
  177. {*}
  178. {* This routine creates a dialog window using the WIND resource template with a}
  179. {* resource ID of windowTmplID in front of all existing windows.  A pointer to}
  180. {* this window is returned.  If we’re running on a Color QuickDraw machine, the}
  181. {* window is created as a color window.  This window must be disposed of by}
  182. {* calling CloseWindow followed by DisposPtr on the window’s DialogRecord.  The}
  183. {* window shouldn’t be closed using DisposeWindow.  When this routine returns,}
  184. {* the new dialog is the current GrafPort.}
  185. {*}
  186. {* The window isn’t explicitly repositioned from its position as defined by the}
  187. {* resource definition.  Instead, I use the window-positioning utilities.  This}
  188. {* isn’t documented at this time and it isn’t even certain the these utilities}
  189. {* will be implemented in the final release of system software 7.0.  Use ResEdit}
  190. {* 2.1’s WIND editor to control the window-positioning utilities.}
  191. {*}
  192. {* If there isn’t enough memory for the window, then gError global is set to}
  193. {* memFullErr.  If the WIND resource couldn’t be loaded, gError is set to}
  194. {* resNotFound.  If anything else went wrong while creating the window, then}
  195. {* gError is set to dsSysErr.}
  196. {*******************************************************************************)
  197.  
  198.     function CreateDialog (dialogTmplID: Integer): DialogPtr;
  199.  
  200.  
  201. implementation
  202.  
  203. (*******************************************************************************}
  204. {* Constants}
  205. {*******************************************************************************)
  206.  
  207.     const
  208.         rOKAlertID = 6010; {Resource ID of alert with OK button}
  209.         rOKCancelAlertID = 6011; {Resource ID of alert with OK and Cancel buttons}
  210.  
  211.         rIconSuiteID = 128; {Resource ID of application icon suite}
  212.         rAppNameString = 0;   {Resource ID of application name}
  213.         rAboutAlert = 258; {Resource ID of About alert}
  214.  
  215.  
  216. (*******************************************************************************}
  217. {* Variables}
  218. {*******************************************************************************)
  219.  
  220.     var
  221.         gNotification: NMRec; {Notification record}
  222.  
  223.  
  224. {$S Main}
  225. (*******************************************************************************}
  226. {* Public: DoQuit}
  227. {*}
  228. {* The gQuitting global variable is set to TRUE.  This causes the main event loop}
  229. {* to terminate on the next iteration.}
  230. {*******************************************************************************)
  231.  
  232.     procedure DoQuit;
  233.  
  234.     begin
  235.         gQuitting := TRUE
  236.     end;
  237.  
  238.  
  239. {$S Main}
  240. (*******************************************************************************}
  241. {* Private: NotifyAlert - Present a notification for an alert}
  242. {*}
  243. {* This routine is called to present a notification to the user in the form of a}
  244. {* flashing icon in the application menu whenever an alert from this program is}
  245. {* displayed.  It’s only necessary to present a notification if this application}
  246. {* is in the background, so the first check that’s done is to compare our own}
  247. {* process serial number against the process serial number of the foreground}
  248. {* process.  If they’re the same, then this application is in the foreground and}
  249. {* no notification is needed.  If they’re not the same, then the notification is}
  250. {* presented.}
  251. {*}
  252. {* After this application is brought to the front and the alert is dismissed this}
  253. {* notification is removed by the routine that calls NotifyAlert.}
  254. {*******************************************************************************)
  255.  
  256.     procedure NotifyAlert;
  257.  
  258.         var
  259.             iconSuite: Handle;      {Handle to the icon suite}
  260.             osEvent: EventRecord; {OS event for resume}
  261.             error: OSErr;
  262.  
  263.         procedure RecoverError (errorCode: OSErr);
  264.  
  265.         begin
  266.             EXIT(NotifyAlert)
  267.         end;
  268.  
  269.     begin
  270.         (* If this application isn’t in front, post a notification *)
  271.         if not gWereInFront then
  272.             begin
  273.                 (* Get the small icon for this application *)
  274.                 iconSuite := Get1Resource('SICN', rIconSuiteID);
  275.                 with gNotification do
  276.                     begin
  277.                         (*WITH*)
  278.                         qType := ORD(nmtype);
  279.                         (*WITH*)
  280.                         nmMark := 1;
  281.                         (*WITH*)
  282.                         nmIcon := iconSuite;
  283.                         (*WITH*)
  284.                         nmSound := Handle(-1);
  285.                         (*WITH*)
  286.                         nmStr := nil;
  287.                         (*WITH*)
  288.                         nmResp := nil;
  289.                         (*WITH*)
  290.                         nmRefCon := 0
  291.                     end;
  292.  
  293.                 (* Post the notification *)
  294.                 error := NMINstall(@gNotification);
  295.  
  296.                 (* Wait for a resume event *)
  297.                 while not WaitNextEvent(app4Mask, osEvent, -1, nil) do (*<*)
  298.                     ;
  299.                 gWereInFront := TRUE;
  300.                 error := NMRemove(@gNotification);
  301.  
  302.                 (* Notification blows away pending update events, so force *)
  303.                 InvalRect(FrontWindow^.portRect);
  304.             end
  305.     end;
  306.  
  307.  
  308. {$S Main}
  309. (*******************************************************************************}
  310. {* Public: ShowStopAlert}
  311. {*}
  312. {* NotifyAlert is called to present a notification to the user in case this}
  313. {* application is in the background at the time of the alert.  This routine then}
  314. {* removes the notification after StopAlert is called.}
  315. {*******************************************************************************)
  316.  
  317.     function ShowStopAlert (messageClass: Integer; messageIndex: Integer): Integer;
  318.  
  319.         var
  320.             aMessage: Str255; {Contents of message to place in alert}
  321.  
  322.     begin
  323.         (* Put the specified message into the dialog parameter text *)
  324.         GetIndString(aMessage, messageClass, messageIndex);(*<*)
  325.         ParamText(aMessage, '', '', '');
  326.  
  327.         (* Show the stop alert *)
  328.         InitCursor;
  329.         NotifyAlert;
  330.         ShowStopAlert := StopAlert(rOKAlertID, nil);
  331.     end;
  332.  
  333.  
  334. {$S Main}
  335. (*******************************************************************************}
  336. {* Public: ShowCautionOKCancelAlert}
  337. {*}
  338. {* NotifyAlert is called to present a notification to the user in case this}
  339. {* application is in the background at the time of the alert.  This routine then}
  340. {* removes the notification after ShowCautionOKCancelAlert is called.}
  341. {*******************************************************************************)
  342.  
  343.     function ShowCautionOKCancelAlert (messageClass: Integer; messageIndex: Integer): Integer;
  344.  
  345.         var
  346.             aMessage: Str255; {Contents of message to place in alert}
  347.  
  348.     begin
  349.         (* Put the specified message into the dialog parameter text *)
  350.         GetIndString(aMessage, messageClass, messageIndex);(*<*)
  351.         ParamText(aMessage, '', '', '');
  352.  
  353.         (* Show the stop alert *)
  354.         InitCursor;
  355.         NotifyAlert;
  356.         ShowCautionOKCancelAlert := CautionAlert(rOKCancelAlertID, nil)
  357.     end;
  358.  
  359.  
  360. {$S Main}
  361. (*******************************************************************************}
  362. {* Public: ShowCautionOKAlert}
  363. {*}
  364. {* NotifyAlert is called to present a notification to the user in case this}
  365. {* application is in the background at the time of the alert.  This routine then}
  366. {* removes the notification after ShowCautionOKAlert is called.}
  367. {*******************************************************************************)
  368.  
  369.     function ShowCautionOKAlert (messageClass: Integer; messageIndex: Integer): Integer;
  370.  
  371.         var
  372.             aMessage: Str255; {Contents of message to place in alert}
  373.             error: OSErr;
  374.  
  375.     begin
  376.         (* Put the specified message into the dialog parameter text *)
  377.         GetIndString(aMessage, messageClass, messageIndex);(*<*)
  378.         ParamText(aMessage, '', '', '');
  379.  
  380.         (* Show the stop alert *)
  381.         InitCursor;
  382.         NotifyAlert;
  383.         ShowCautionOKAlert := CautionAlert(rOKAlertID, nil);
  384.         error := NMRemove(@gNotification)
  385.     end;
  386.  
  387.  
  388. {$S Main}
  389. (*******************************************************************************}
  390. {* Public: ShowAboutBox}
  391. {*}
  392. {* The name and the version number of this application are retrieved from}
  393. {* resources.  They’re then displayed in the About box using the ParamText}
  394. {* mechanism.}
  395. {*******************************************************************************)
  396.  
  397.     procedure ShowAboutBox;
  398.  
  399.         var
  400.             appNameRes: StringHandle; {Handle to name of application}
  401.             curVersion: VersRecHndl;  {Handle to version record of this app}
  402.             appName: Str255;       {Name of this application}
  403.             verNum: Str255;       {Long version number of this application}
  404.             itemHit: Integer;      {Item number of clicked alert item; ignored}
  405.  
  406.     begin
  407.         (* Get the name of this application *)
  408.         appNameRes := GetString(rAppNameString);
  409.         if appNameRes <> nil then
  410.             appName := appNameRes^^
  411.         else
  412.             appName := '?';
  413.  
  414.         (* Get the version information of this application *)
  415.         curVersion := VersRecHndl(Get1Resource('vers', 1));
  416.         if curVersion <> nil then
  417.             (* Get the long version number *)
  418.             verNum := StringPtr(ORD(@curVersion^^.shortVersion) + ORD(curVersion^^.shortVersion[0]) + 1)^
  419.         else
  420.             verNum := '?';
  421.  
  422.         (* Show the About alert *)
  423.         ParamText(appName, verNum, '', '');
  424.         itemHit := Alert(rAboutAlert, nil);
  425.     end;
  426.  
  427.  
  428. {$S Main}
  429. (*******************************************************************************}
  430. {* Public: CreateWindow}
  431. {*}
  432. {* A WindowRecord is allocated on the heap before GetNewWindow is called.  This}
  433. {* is done to reduce heap fragmentation.}
  434. {*******************************************************************************)
  435.  
  436.     function CreateWindow (windowTmplID: Integer): WindowPtr;
  437.  
  438.         var
  439.             windStore: Ptr;       {Pointer to window record}
  440.             aWindow: WindowPtr; {Pointer to the new window}
  441.             qdVersion: LongInt;   {Version of QuickDraw on this machine}
  442.             result: OSErr;
  443.  
  444.         procedure RecoverError (error: Integer);
  445.  
  446.         begin
  447.             if aWindow <> nil then
  448.                 CloseWindow(aWindow);
  449.             if windStore <> nil then
  450.                 DisposPtr(windStore);
  451.             gError := error;
  452.             CreateWindow := nil;
  453.             EXIT(CreateWindow)
  454.         end;
  455.  
  456.     begin
  457.         windStore := nil;
  458.         aWindow := nil;
  459.  
  460.         (* Allocate window record *)
  461.         windStore := NewPtrMargin(SIZEOF(WindowRecord), kAllocApp, not kAllocClr);
  462.  
  463.         (* Create the new window *)
  464.         if windStore = nil then
  465.             RecoverError(memFullErr)
  466.         else
  467.             begin
  468.                 (* Create the new window *)
  469.                 result := Gestalt(gestaltQuickdrawVersion, qdVersion); (*<*)
  470.                 if qdVersion = gestaltOriginalQD then
  471.                     aWindow := GetNewWindow(windowTmplID, windStore, WindowPtr(-1))
  472.                 else
  473.                     aWindow := GetNewCWindow(windowTmplID, windStore, WindowPtr(-1));
  474.                 if FailLowMemory(0) then
  475.                     RecoverError(memFullErr)
  476.                 else if ResError <> noErr then
  477.                     if ResError = memFullErr then
  478.                         RecoverError(memFullErr)
  479.                     else if (ResError = noErr) | (ResError = resNotFound) then
  480.                         RecoverError(resNotFound)
  481.                     else
  482.                         RecoverError(dsSysErr);
  483.  
  484.                 SetPort(aWindow);
  485.                 SetWRefCon(aWindow, 0)
  486.             end;
  487.         CreateWindow := aWindow
  488.     end;
  489.  
  490.  
  491. {$S Main}
  492. (*******************************************************************************}
  493. {* Public: CreateDialog}
  494. {*}
  495. {* A DialogRecord is allocated on the heap before GetNewWindow is called.  This}
  496. {* is done to reduce heap fragmentation.}
  497. {*******************************************************************************)
  498.  
  499.     function CreateDialog (dialogTmplID: Integer): DialogPtr;
  500.  
  501.         var
  502.             dlogStore: Ptr;       {Pointer to dialog record}
  503.             aDialog: DialogPtr; {Pointer to the new dialog window}
  504.             qdVersion: LongInt;   {Version of QuickDraw on this machine}
  505.             result: OSErr;
  506.  
  507.         procedure RecoverError (error: Integer);
  508.  
  509.         begin
  510.             if aDialog <> nil then
  511.                 CloseWindow(aDialog);
  512.             if dlogStore <> nil then
  513.                 DisposPtr(dlogStore);
  514.             gError := error;
  515.             CreateDialog := nil;
  516.             EXIT(CreateDialog)
  517.         end;
  518.  
  519.     begin
  520.         dlogStore := nil;
  521.         aDialog := nil;
  522.  
  523.         (* Allocate window record *)
  524.         dlogStore := NewPtrMargin(SizeOf(DialogRecord), kAllocApp, not kAllocClr);
  525.  
  526.         (* Create the new window *)
  527.         if dlogStore = nil then
  528.             RecoverError(memFullErr)
  529.         else
  530.             begin
  531.                 result := Gestalt(gestaltQuickdrawVersion, qdVersion); (*<*)
  532.                 if qdVersion = gestaltOriginalQD then
  533.                     aDialog := GetNewWindow(dialogTmplID, dlogStore, WindowPtr(-1))
  534.                 else
  535.                     aDialog := GetNewCWindow(dialogTmplID, dlogStore, WindowPtr(-1));
  536.                 if FailLowMemory(0) then
  537.                     RecoverError(memFullErr)
  538.                 else if ResError <> noErr then
  539.                     if ResError = memFullErr then
  540.                         RecoverError(memFullErr)
  541.                     else if (ResError = noErr) | (ResError = resNotFound) then
  542.                         RecoverError(resNotFound)
  543.                     else
  544.                         RecoverError(dsSysErr);
  545.  
  546.                 SetPort(aDialog);
  547.                 SetWRefCon(aDialog, 0)
  548.             end;
  549.         CreateDialog := aDialog
  550.     end;
  551.  
  552. end.